var cc__importDefault = __importDefault;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $GameEventType = require("GameEventType");
var $1$EventManager = require("EventManager");
var r = cc__importDefault($1$EventManager);
var $GlobalEnum = require("GlobalEnum");
var $1$GameConfig = require("GameConfig");
var s = cc__importDefault($1$GameConfig);
var def_AudioManager = function () {
  function _ctor() { }
  _ctor.init = function () {
    this.playingEffectCount = 0;
    cc.audioEngine.setMusicVolume(1);
    cc.audioEngine.setEffectsVolume(1);
    this.onEvents();
  };
  _ctor.onEvents = function () {
    r.default.on($GameEventType.EventType.AudioEvent.playBGM, this.playBGM, this);
    r.default.on($GameEventType.EventType.AudioEvent.playClickBtn, this.playClickBtn, this);
    r.default.on($GameEventType.EventType.AudioEvent.playEffect, this.playEffect, this);
    r.default.on($GameEventType.EventType.AudioEvent.stopBGM, this.stop, this);
    r.default.on($GameEventType.EventType.AudioEvent.pause, this.pauseBGM, this);
    r.default.on($GameEventType.EventType.AudioEvent.resume, this.resumeBGM, this);
  };
  _ctor.playClickBtn = function () {
    this.playEffect($GlobalEnum.GlobalEnum.AudioClip.clickBtn);
  };
  _ctor.playEffect = function (t) {
    // 检查并加载音效资源
    const clipKey = t; // 用于引用音效资源的键
    const clips = this.allClips;

    // 如果音效资源未加载，则进行加载
    if (clips[clipKey] === undefined) {
      cc.loader.loadRes(clipKey, cc.AudioClip, (error, audioClip) => {
        if (error) {
          clips[clipKey] = null; // 记录为未找到
          return cc.warn("要播放的音效资源未找到：", clipKey);
        }
        clips[clipKey] = audioClip; // 加载成功，保存音效资源
        this._playEffect(clipKey); // 播放音效
      });
    } else {
      this._playEffect(clipKey); // 已加载，直接播放
    }

  };
  _ctor._playEffect = function (t) {
    // 检查并播放音效效果
    const audioConfig = s.default.audioConfig;
    const audioClip = this.allClips[t];

    // 确保音效效果已配置，音效已加载，并且播放数量未超过最大限制
    if (audioConfig.effect && audioClip !== null && this.playingEffectCount < this.maxEffectCount) {
      const audioId = cc.audioEngine.play(audioClip, false, 1); // 播放音效
      cc.audioEngine.setFinishCallback(audioId, this._effectFinish.bind(this)); // 设置音效完成回调
    }

  };
  _ctor._effectFinish = function () {
    this.playingEffectCount--;
    this.playingEffectCount < 0 && (this.playingEffectCount = 0);
  };
  _ctor.playBGM = function (t, e) {
    // 播放背景音乐
    undefined === e && (e = true);
    const clipKey = t; // 用于引用音效资源的键
    const clips = this.allClips;

    // 如果音效资源未加载，则进行加载
    if (clips[clipKey] === undefined) {
      cc.loader.loadRes(clipKey, cc.AudioClip, (error, audioClip) => {
        if (error) {
          clips[clipKey] = null; // 记录为未找到
          return cc.warn("要播放的音效资源未找到：", clipKey);
        }
        clips[clipKey] = audioClip; // 加载成功，保存音效资源
        this._playBGM(clipKey, e); // 播放背景音乐
      });
    } else {
      this._playBGM(clipKey, e); // 已加载，直接播放
    }

  };
  _ctor._playBGM = function (t, e) {
    if (s.default.audioConfig.bgm && null !== this.allClips[t]) {
      cc.audioEngine.stopMusic();
      cc.audioEngine.playMusic(this.allClips[t], e);
    }
  };
  _ctor.stop = function () {
    cc.audioEngine.stopMusic();
  };
  _ctor.pauseBGM = function () {
    cc.audioEngine.pauseMusic();
  };
  _ctor.resumeBGM = function () {
    cc.audioEngine.resumeMusic();
  };
  _ctor.allClips = {};
  _ctor.playingEffectCount = 0;
  _ctor.maxEffectCount = 2;
  return _ctor;
}();
exports.default = def_AudioManager;